<script type="text/javascript">
$(function(){
	var pg = App.soagov.comEdit.ui;
	var frm = {
		code: pg.seek('code'),
		name: pg.seek('name'),
//		faceId: pg.seek('faceId'),
		faceCode: pg.seek('faceCode'),
		faceName: pg.seek('faceName'),
// 		faceVer: pg.seek('faceVer'),
// 		faceEntry: pg.seek('faceEntry'),
		impl: pg.seek('impl'),
		ver: pg.seek('ver'),
		status: pg.seek('status'),
		memo: pg.seek('memo')
	};
	frm.status.mac('combo', {
		width: 338,
		boxWidth: 339,
		readOnly: 1,
		data: Cache.base.com.status
	});
	var faceGd = $('<div class="grid"></div>').mac('grid', {
		key: 'id',
		cols : [{
			field: 'code', title : '接口英文名', width: 140,
			render: function(r){
				return $('<span class="a"></span>').click(function(){
					App.open({
						ctx:'soagov',
						action: 'faceView',
						params: { id: r.id }
					});
				}).append(r.code);
			}
		},{
			field: 'name', title : '接口中文名', width: 240
		},{
			field: 'ver', title : '版本', width: 80
		}],
		loader: {
			url: App.ctx + '/face/list.do',
			params: { pageNo: 1, pageSize: 50 },
			autoLoad: true
		},
		pagerLength: 10
	});
	frm.faceCode.mac('combo', {
		keyField: 'id',
		displayField: 'code',
		multiSelect: false,
		width: 338,
		boxWidth: 490,
		boxHeight: 300,
		boxEl: faceGd,
		onSelect: function(me, sk, sd){
			var r = sd[0];
			frm.faceName.text(r ? (r.name + ' ' + r.ver) : '');
		}
	});
	frm.faceCode.mac('combo_grid', {});
	
	frm.faceName.click(function(){
		App.open({
			ctx:'soagov',
			action: 'faceView',
			params: { id: frm.faceCode.val() }
		});
	});

	function getValues(){
		return {
			id: pg.params.id,
			parent: pg.params.parent,
			code: frm.code.val(),
			name: frm.name.val(),
			//faceId: frm.faceId.val(),
			faceId: frm.faceCode.find('input').val(),
			impl: frm.impl.val(),
			ver: frm.ver.val(),
			memo: frm.memo.val()
		};
	}
	function setValues(o){
		pg.seek('title').html(o.id? '编辑组件':'添加组件');
		frm.code.val(o.code);
		frm.name.val(o.name);
		if (o.face) {
			var f = o.face;
			frm.faceCode.val(f.id, f.code);
			frm.faceName.text(f.name + ' ' + f.ver);
// 			frm.faceEntry.text(o.face.entry);
// 			frm.faceVer.val(o.face.ver);
		}
		frm.impl.val(o.impl);
		frm.ver.val(o.ver);
		frm.status.val(o.status);
		frm.memo.val(o.memo);
	}
	function validate(p){
		frm.code.next('.msg').html('*');
		frm.name.next('.msg').html('*');
		frm.faceCode.next('.msg').html('*');
		var x = App.validate2, v= App.soagov.validation;
		alert(JSON.stringify(p));
		return x(v.com.code, frm.code, p.code) & x(v.com.name, frm.name, p.name)
				&x(v.com.faceCode,frm.faceCode,p.faceId);
	}
	pg.seek('pickFace').click(function(){
		App.open({
			ctx: 'base',
			action: 'dirPicker',
			title: '选择一个接口',
			allow: [ C.FACE ],
			model: true
		}, function(d){
			//frm.faceId.val(d.key);
			frm.faceCode.val(d.key, d.code);
			frm.faceName.text(d.title);
// 			$.get(App.ctx + '/face/get.do', {
// 				id: d.key
// 			}, function(rs){
// 				var ro = mac.eval(rs);
// 				if (ro.success) {
// 					o = ro.data;
// 					frm.faceVer.val(o.ver);
// 				}
// 			});
		});
	});
	pg.seek('ok').click(function(){
		var pms = getValues();
		if(!validate(pms))
			return;
		mac.alert('Online demo do not allow this action');
		return false;
		//
		$.post(App.ctx + '/com/save.do', pms, function(rs){
			var ro = mac.eval(rs);
			if (ro.success) {
				pg.callback(ro.data);//更新树	
				if(pg.close){
					pg.close();
				}else{
					pg.callback(null, pg.params.id || pg.params.parent);
				}
			} else {
				pg.seek('msg').html(mac.msg(ro.data, ro.params));
			}
		});
		return false;
	});

// 	if (pg.params.id) {
// 		var w = mac.wait('Loading');
// 		try {
// 			//暂时不允许改CODE
// 			frm.code.attr('readonly', true);
// 			$.get(App.ctx + '/com/get.do', {
// 				id: pg.params.id
// 			}, function(rs) {
// 				var ro = mac.eval(rs);
// 				if (ro.success) {
// 					setValues(ro.data);
// 				} else {
// 					pg.seek('msg').html(mac.msg(ro.data, ro.params));
// 				}
// 			});
// 		} finally {
// 			w.close();
// 		}
// 	} else {
		setValues(pg.params);
// 	}
});
</script>
<div class="form">
	<div class="title">
		<span class="value" name="title"></span>
	</div>
	<div class="item">
		<span class="label">英文名</span> <input type="text" name="code"
			maxlength="32" /> <span class="msg">*</span>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">中文名</span> <input type="text" name="name"
			maxlength="32" /> <span class="msg">*</span>
		<div class="clear"></div>
	</div>
	<div class="item">
		<div class="label">&nbsp;</div>
		<div class="value">
			<span class="a hidden" name="newFace">新的接口</span>&nbsp;|&nbsp; <span
				class="a" name="pickFace">选择接口</span>
		</div>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">接口编号：</span>
		<div class="combo fl" name="faceCode"></div>
		<span class="msg">*</span>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">接口中文名：</span> <span class="value a"
			name="faceName"></span>
		<div class="clear"></div>
	</div>
	<!-- 	<div class="item"> -->
	<!-- 		<span class="label">接口版本：</span> -->
	<!-- 		<input type="text" name="faceVer" readonly /> -->
	<!-- 	</div> -->
	<!-- 	<div class="item"> -->
	<!-- 		<span class="label">入口：</span> -->
	<!-- 		<input type="text" name="faceEntry" /> -->
	<!-- 		<span class="msg">*</span> -->
	<!-- 	</div> -->
	<div class="item">
		<span class="label">实现：</span> <input type="text" name="impl" />
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">版本：</span> <input type="text" name="ver" />
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">状态：</span>
		<div class="combo fl" name="status"></div>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">描述：</span>
		<textarea name="memo"></textarea>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">&nbsp;</span>
		<div name="msg"></div>
		<div class="clear"></div>
	</div>
	<div class="bottom">
		<button name="ok">保存</button>
	</div>
</div>